import publics from './public.js'
import Vue from 'vue'
import Vuex from 'vuex'
var token;
var data = {
	token: '',
	did: ""
}
Vue.use(Vuex)
const ajax = (opt) => {
	uni.chooseImage({
		count: 1,
		sizeType: ['compressed '],
		sourceType: opt.zhangtai || ['album', 'camera'],
		success(res) {
			var imgFiles = res.tempFilePaths[0];
			var uper = uni.uploadFile({
				url: publics.hosturl + 'upload/fileUpload',
				header: {
					'Authorization':uni.getStorageSync('token_token')
				},
				filePath: imgFiles,
				name: 'fileName',
				success(res1) {
					console.log('aaaaaa')
					console.log(res1)
					
					let str = res1.data
							let str1 = str.replace(/\ufeff/g,"") // step1: 首先去掉两头的"",
							let data_data = JSON.parse(str1) // step2: 转换成JSON格式的数据
							console.log('data')
							console.log(data_data)
					
					// var bb = eval('(' + res1.data + ')')
					// console.log(bb)
					if (data_data.code == 1) {
						opt.success(data_data.data);
					}else{
						uni.showToast({
							title: bb.msg,
							icon: "none"
						});
					}
					

					uni.hideLoading();
				},
				fail: function() {
					uni.showToast({
						title: '上传失败',
						icon: "none"
					});
					uni.hideLoading();
				}
			});
			uper.onProgressUpdate(function(res) {
				// 进度条等于 上传到的进度
				// tt.percent = res.progress
				// console.log('上传进度' + res.progress)
				// console.log('已经上传的数据长度' + res.totalBytesSent)
				// console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend)
			})
		}
	})
}




const touxaing = (opt) => {
	uni.chooseImage({
		count: 1,
		sizeType: ['compressed '],
		sourceType: ['album', 'camera'],
		success(res) {
			opt.success(res);
		}
	})
}

export default {
	ajax, //数据请求
	touxaing
}
